package com.anbinh.doctor.fragment.account;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.PricesAdapter;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.ResponseDoctorPrice;
import com.anbinh.doctor.model.ResponseDoctorPrice.Price;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import com.mobeta.android.dslv.DragSortListView;

public class FragmentBangGia extends Fragment implements IRcallBak {
	private static FragmentBangGia fragment = null;
	private View mView;
	private List<Price> mList;
	PricesAdapter adapter;
	private CircleProgressBar loadding;
	private Price mCPrice;
	private DragSortListView mListview;
	private UserLogin userLogin;

	public static FragmentBangGia newInstance() {
		if (fragment == null) {
			fragment = new FragmentBangGia();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.warp_main, null);
		return mView;
	}

	private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
		@Override
		public void drop(int from, int to) {
			Price item = adapter.getItem(from);

			adapter.notifyDataSetChanged();
			adapter.remove(item);
			adapter.insert(item, to);
			onUpdowPrice(item);

		}
	};

	private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener() {
		@Override
		public void remove(int which) {
			onDelete(adapter.getItem(which));
			adapter.remove(adapter.getItem(which));
		}
	};

	private DragSortListView.DragScrollProfile ssProfile = new DragSortListView.DragScrollProfile() {
		@Override
		public float getSpeed(float w, long t) {
			if (w > 0.8f) {
				// Traverse all views in a millisecond
				return ((float) adapter.getCount()) / 0.001f;
			} else {
				return 10.0f * w;
			}
		}
	};

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		mListview = (DragSortListView) mView.findViewById(R.id.listDrag);
		mListview.setDropListener(onDrop);
		mListview.setRemoveListener(onRemove);
		mListview.setDragScrollProfile(ssProfile);
		mListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				mCPrice = mList.get(position);
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, Utils.EDIT_BANGGIA);
				bundle.putBoolean(Utils.CUSTOMBAR, true);
				bundle.putInt(Utils.REQUEST_CODE, Utils.RESUME_PRICE);
				bundle.putString(Utils.TITLE,
						getResources().getString(R.string.cap_nhat_bang_gia));
				Utils.getNewIntent(getActivity(), bundle);
			}

		});
		onLoadData();
	}

	protected void onUpdowPrice(Price item) {

		ServerController.getInstance(getActivity()).requestChangeLocationPrice(
				userLogin.getData().getToken(), getListSpecial(),
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						//Utils.showMessage(getActivity(), data.getDescription());
					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);

					}
				});
	}

	public void showEdit(boolean isShow) {
		for (int i = 0; i < mList.size(); i++) {
			mList.get(i).setEdit(isShow);
		}
		adapter.notifyDataSetChanged();
	}

	public void onLoadData() {

		loadding.setVisibility(View.VISIBLE);
		ServerController.getInstance(getActivity()).requestGetListPrice(
				userLogin.getData().getToken(),
				new OnRequest<ResponseDoctorPrice>() {

					@Override
					public void onSuccess(ResponseDoctorPrice data) {
						loadding.setVisibility(View.INVISIBLE);
						if (getActivity() == null)
							return;

						if (data.getResponsecode() >= 0) {
							mList = data.getData();
							adapter = new PricesAdapter(getActivity(),
									R.layout.item_banggia, mList);
							mListview.setAdapter(adapter);

						}

					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.INVISIBLE);
					}
				});
	}

	private String getListSpecial() {
		String result = "";
		for (int i = 0; i < adapter.getCount(); i++) {
			result = result + "," + adapter.getItem(i).getId();
		}

		return result.substring(1, result.length());
	}

	protected void onDelete(final Price item) {
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		ServerController.getInstance(getActivity()).requestdoRemovePrice(
				userLogin.getData().getToken(), item.getId(),
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {

					}

					@Override
					public void onError(int errorCode, String message) {
						// TODO Auto-generated method stub

					}
				});
	}

	public Price getCurrentPrice() {
		return mCPrice;
	}

	@Override
	public void onDisplay(int type, Bundle data) {

	}

}
